/**
 * Created by zhanglihe on 15/11/10.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'validation'
], function( $, common, ready, Collection, template, Model, dialog, Validation ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'goods_attrs',
                condition: {limit: 20, offset: 0},
                model: {
                    url: _global.url.api + 'goods_attr/{id}',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove'
                    },
                    edit: function(e) {
                        e.preventDefault();
                        _this.data = this.data;
                        var model = this;

                        var options = '';
                        for(var i = 0; i < model.data.options.length; i++){
                            options += model.data.options[i];
                            options += (i + 1) < model.data.options.length ? "\n" : '';
                        }
                        _this.dialog = dialog({
                            content: template('add-tpl', {name: this.data.name, options: options}),
                            width: 500,
                            ok: function() {
                                if(valid.isFail()) {
                                    return false;
                                }
                                model.data.name = $('[name="name"]').val();
                                model.data.options = $('[name="options"]').val().split("\n");
                                if(!model.put()) {
                                    return false;
                                }
                            },cancel: function() {
                                _this.dialog = null;
                            }
                        });
                        var valid = new Validation({
                            rules: {
                                name: {required: true},
                                options: {required: true}
                            }
                        });
                        _this.dialog.show();
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                model.del();
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    content: template('add-tpl', {}),
                    width: 500,
                    ok: function() {
                        if(valid.isFail()) {
                            return false;
                        }
                        _this.data.options = _this.data.options.split("\n");
                        if(new Model({
                                url: _global.url.api + 'goods_attr',
                                data: _this.data,
                                valid_config: {
                                    name: 'required',
                                    options: 'required'
                                }
                            }).post()) {
                            _this.collection.reload();
                        } else {
                            return false;
                        }
                    },cancel: function() {}
                });
                _this.dialog.show();

                var valid = new Validation({
                    rules: {
                        name: {required: true},
                        options: {required: true}
                    }
                });

                $('input,select,textarea').on('change', function(e) {
                    _this.change(e);
                });
            });
        },

        change: function(e) {
            console.log(this);
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});